<?php
namespace app\massage\controller;
use app\AdminRest;


use app\ApiRest;
use app\massage\model\ArticleList;
use app\massage\model\CoachLevel;
use app\massage\model\CommissionType;
use app\massage\model\FieldList;
use app\massage\model\Identity;
use app\massage\model\SubData;
use app\massage\model\SubList;
use think\App;

use app\massage\model\Printer as model;

use think\facade\Db;


class IndexIdentity extends ApiRest
{
    protected $model;
    protected $commission_type_model;
    protected $level_model;
//    protected $noNeedLogin = ['list'];

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new Identity();
        $this->commission_type_model = new CommissionType();
        $this->level_model = new CoachLevel();
    }

    public function list()
    {
        $input = $this->_param;
        $type = $input['type'] ?? 1;
        $dis = [
            'type' => $type,
            'status' => 1
        ];
        $data = $this->model->dataList($dis);
        $type_names = $this->commission_type_model->column('name','id');

        foreach ($data as &$item) {
            $arr = json_decode($item['rule'],true);
            $rule = [];
            foreach ($arr as $val) {
                $rule[] = [
                    'type_id' => $val['type_id'],
                    'type_name' => $type_names[$val['type_id']] ?? '',
                    'ratio' => $val['ratio'],
                ];
            }
            $item['rule'] = $rule;
            if($item['type'] == 1){
                $level = $this->level_model->where(['status' => 1])
                    ->order('star','asc')
                    ->column('title,star,balance');
            }
            $item['level'] = $level ?? [];
        }
        return $this->success($data);
    }
}
